package crontab

import (
	"context"
	global2 "longmen/server/config/global"
	"longmen/server/pkg/common/helper/const"
	"time"

	"github.com/olivere/elastic/v7"
)

func DoCleanEsData() {
	cleanMatchTextLive()
	cleanChatHistory()
	cleanOnlineDevice()
}

//清除文字直播
func cleanMatchTextLive() {
	global2.Log.Info("CleanMatchTextLive start...")
	t := time.Now().AddDate(0, 0, -15).UnixNano()
	q := elastic.NewBoolQuery().Filter(elastic.NewRangeQuery("create_time").Lt(t))
	//篮球
	do, err := global2.GetEs().DeleteByQuery("basketball_text_live").Query(q).Do(context.Background())
	if err != nil {
		global2.Log.Errorf("Clean basketball_text_live delete failed : %v", err)
	} else {
		global2.Log.Infof("Clean basketball_text_live delete success, count : %v", do.Total)
	}

	//足球
	do2, err := global2.GetEs().DeleteByQuery("football_text_live").Query(q).Do(context.Background())
	if err != nil {
		global2.Log.Errorf("Clean football_text_live delete failed : %v", err)
	} else {
		global2.Log.Infof("Clean football_text_live delete success, count : %v", do2.Total)
	}

	global2.Log.Info("CleanMatchTextLive end...")
}

func cleanChatHistory() {
	global2.Log.Info("CleanChatHistory start...")
	t := time.Now().Unix() - 60*60*24
	q := elastic.NewBoolQuery().Filter(elastic.NewRangeQuery("create_time").Lt(t))
	do, err := global2.GetEs().DeleteByQuery(_const.ES_CHAT_HISTORY).Query(q).Do(context.Background())
	if err != nil {
		global2.Log.Errorf("CleanChatHistory delete failed : %v", err)
	} else {
		global2.Log.Infof("CleanChatHistory delete success, count : %v", do.Total)
	}
	global2.Log.Info("CleanChatHistory end...")
}

func cleanOnlineDevice() {

}
